import 'dart:async';

import 'package:erp/UI/CodeResources/Screen.dart';
import 'package:erp/UI/Components/PageFrame.dart';
import 'package:erp/UI/Components/QuestionDialog.dart';
import 'package:flutter/material.dart';
import "../CodeResources/Localization.dart";
import "./UserCenterPage.dart";
import './HomePage.dart';
import '../Routes.dart';

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MainPageStateful();
}

class MainPageStateful extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MainPageState();
}

class MainPageState extends State<MainPageStateful> {
  var pageController = PageController();
  var _currentPageIndex = 0;

  @override void dispose() {
    super.dispose();
    pageController.dispose();
  }

  @override
  void deactivate() {

    super.deactivate();
  }


  @override
  Widget build(BuildContext context) {

    var currentLocale = AppLocalizations.of(context);

    _onPageIndexChanged(int pageIndex, {bool ignoteGotoPage = false}){
      setState(() {
        _currentPageIndex = pageIndex;
        if (!ignoteGotoPage) pageController.animateToPage(_currentPageIndex, duration: Duration(milliseconds: 200), curve: Curves.easeOut);
      });
    }

    return WillPopScope(
        onWillPop: () async {
          return showQuestionDialog(context, title: "ئەسكەرتىش", content: "ئەپتىن چىكىنمەكچى بولۇۋاتىسىز، راستىنلا چىكىنەمسىز؟");
        },
        child: Directionality(
        textDirection: currentLocale.textDirection,
        child: PageFrame(
          child: Scaffold(
            body: PageView(
              controller: pageController,
              onPageChanged: (pageIndex){
                _onPageIndexChanged(pageIndex, ignoteGotoPage: true);
              },
              children: [
                HomePage(),
                Center(
                  child: RaisedButton(onPressed: (){

                  }, child: Text("Click me to exit"),),
                ),
                UserCenter()
              ],
            ),
            bottomNavigationBar: BottomNavigationBar(
                currentIndex: _currentPageIndex,
                onTap: _onPageIndexChanged,
                items: [
                  BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("باش بەت")),
                  BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text("Apps")),
                  BottomNavigationBarItem(icon: Icon(Icons.person), title: Text("ئەزا مەركىزى")),
                ]
            ),
          ),
        ), )
    );

  }
}

